package com.tg.app.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.demo.view.SettingItemTextView;
import com.tg.app.R;
import com.tg.app.bean.DevicePresetPoints;
import com.tg.app.camera.Camera;
import com.tg.app.util.ObjectBoxUtil;
import com.tg.appcommon.android.ResourcesUtil;
import com.tg.appcommon.android.TGLog;

import java.util.ArrayList;
import java.util.List;

public class CruisePrePositionAdapter extends BaseAdapter {
    public static final String TAG = CruisePrePositionAdapter.class.getSimpleName();
    public static final String MODE_DIALOG = "mode_dialog";

    public void setMaxNumber(int maxNumber) {
        this.mMaxNumber = maxNumber;
    }

    private int mMaxNumber = 0;

    protected Camera mCamera;
    protected final List<DevicePresetPoints> prePositions = new ArrayList<>();
    protected final List<DevicePresetPoints> selectedPresetPoints = new ArrayList<>();
    protected final Context context;
    protected boolean isEditEnable = false;
    protected final PrePositionAdapter.PrePositionListener mPrePositionListener;
    protected int mIdleTime = 30;
    protected int mWatchPosNumber = -1;
    protected int mSelectPosition = -1;


    public int getWatchPosNumber() {
        return mWatchPosNumber;
    }
    public void setWatchPosInfo(int watchPosNumber, int idleTime) {
        if (mWatchPosNumber == watchPosNumber){
            return;
        }

        mWatchPosNumber = watchPosNumber;
        mIdleTime = idleTime;
        notifyDataSetChanged();
    }

    public int getSelectPosition() {
        return mSelectPosition;
    }
    public int getIdleTime() {
        return mIdleTime;
    }
    public void setCamera(Camera camera){
        this.mCamera = camera;
    }
    public String getMode(){
        return MODE_DIALOG;
    }
    public List<DevicePresetPoints> getSelectedPresetPoints() {
        return selectedPresetPoints;
    }

    public List<DevicePresetPoints> getPrePositions() {
        return prePositions;
    }

    public void clearSelectedPresetPoints(){
        selectedPresetPoints.clear();
    }


    public boolean isExistDevicePresetPoints(int num) {
        for (DevicePresetPoints p : prePositions) {
            if (p.num == num){
                return true;
            }
        }
        return false;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void clearPointFromSelected(){
        for (DevicePresetPoints p : selectedPresetPoints){
            TGLog.d(TAG,"number  = " + p.num + "mWatchPosNumber = " + mWatchPosNumber);
            if (mWatchPosNumber == p.num){
                mWatchPosNumber = -1;
                mIdleTime = 30;
            }
            prePositions.remove(p);

        }
        notifyDataSetChanged();
    }

    public void addPointFromSelected(int position){
        DevicePresetPoints devicePresetPoints = getDevicePresetPoints(position);
        selectedPresetPoints.add(devicePresetPoints);

    }
    public boolean isEditEnable() {
        return isEditEnable;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setData(String uuid){
        List<DevicePresetPoints> points = ObjectBoxUtil.getDevicePresetPoints(uuid);
        if (points!=null){
            prePositions.addAll(points);
            notifyDataSetChanged();
        }
    }

    public void setData(List<DevicePresetPoints> list){
        prePositions.clear();
        prePositions.addAll(list);
        notifyDataSetChanged();
    }

    public void addData(DevicePresetPoints devicePresetPoints){
        prePositions.add(devicePresetPoints);
        notifyDataSetChanged();
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setEditEnable(boolean editEnable) {
        isEditEnable = editEnable;
        if (isEditEnable){
            mSelectPosition = -1;
        }
        notifyDataSetChanged();
    }

    public CruisePrePositionAdapter(Context context, PrePositionAdapter.PrePositionListener listener) {
        this.context = context;
        mPrePositionListener = listener;

    }

    public int getPrePositionState(int position){
        if (position + 1 == mWatchPosNumber){
            return 2;
        }

        for (DevicePresetPoints p : prePositions) {
            if (p.num == position + 1){
                return 1;
            }
        }
        return 0;
    }


    private boolean isSetting(int position){
        TGLog.d(TAG, "position = " + position);
        for (int i = 0; i < prePositions.size(); i++){
            TGLog.d(TAG, "prePositions.get(i).num = " + prePositions.get(i).num);
            if (position == prePositions.get(i).num - 1){
                return true;
            }
        }
        return false;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.layout_preposition_criuse, null);
        }
        SettingItemTextView settingItemTextView = (SettingItemTextView)convertView;
        boolean set = isSetting(position);
        settingItemTextView.setNumber(String.valueOf(position+1));
        settingItemTextView.setRightArrow(R.drawable.ic_tange_global_more);
        settingItemTextView.showContent(false);
        settingItemTextView.setNameColor(ResourcesUtil.getResources().getColor(R.color.global_customize_fixed_color_black));
        settingItemTextView.setNumberColor(ResourcesUtil.getResources().getColor(R.color.global_customize_fixed_color_black));
        if (set){
            settingItemTextView.setBackgroundResource(R.drawable.shape_white_r10);
            if (mWatchPosNumber != -1 && mWatchPosNumber == position + 1){
                String text = ResourcesUtil.getString(R.string.device_function_setting) + "(" + ResourcesUtil.getString(R.string.txt_preposition_tip) + ")";
                settingItemTextView.setName(text);
            }else{
                settingItemTextView.setName(R.string.device_function_setting);
            }
        }else{
            settingItemTextView.setName(R.string.device_function_no_setting);
            settingItemTextView.setBackgroundResource(R.drawable.shape_gray_r10);
        }

        settingItemTextView.getRightArrow().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mPrePositionListener != null){
                    mPrePositionListener.click(position);
                }
            }
        });

        return convertView;
    }


    public DevicePresetPoints getDevicePresetPoints(int position){
        for (DevicePresetPoints devicePresetPoints : prePositions){
            if (devicePresetPoints.num == position + 1){
                return devicePresetPoints;
            }
        }
        return null;
    }


    @Override
    public int getCount() {
        return mMaxNumber;
    }

    @Override
    public Object getItem(int i) {
        if (i  < 0 || i >= prePositions.size()){
            return null;
        }
        for (int j = 0; j < prePositions.size(); j++){
            if (j == prePositions.get(j).num - 1){
                return prePositions.get(j);
            }
        }
        return prePositions.get(i);
    }

    @Override
    public long getItemId(int i) {
        Object object = getItem(i);
        if (object == null){
            return 0;
        }
        return ((DevicePresetPoints)object).id;
    }
}


